package com.chixing.service.impl;

import com.chixing.mapper.FlowMapper;
import com.chixing.mapper.MyorderMapper;
import com.chixing.pojo.Flow;
import com.chixing.pojo.FlowExample;
import com.chixing.pojo.Myorder;
import com.chixing.service.FlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FlowServiceImpl implements FlowService{

    @Autowired
    private FlowMapper flowMapper;
    @Autowired
    private MyorderMapper myorderMapper;


    @Override
    public Flow getById(Integer flowId) {

        Flow flow = flowMapper.selectByPrimaryKey (flowId);
       return flow;

    }

    @Override
    public boolean save(Flow flow) {
        flow.setFlowNum (flow.getFlowNum ());
        //1.添加流水
        int rows = flowMapper.insertSelective (flow);
        System.out.println ("insert flow rows:" + rows);
        //2.修改订单状态
        Myorder myorder= myorderMapper.selectByPrimaryKey (Integer.valueOf (flow.getMyorderId ()));
//        Myorder myorder = new Myorder ();
        myorder.setMyorderStatus (1);//已支付
        int updateRows = myorderMapper.updateByPrimaryKeySelective (myorder);
        System.out.println ("订单状态修改update order status:"+updateRows);
        return true;
    }

    @Override
    public Flow getByOrderId(String orderId) {
        FlowExample example = new FlowExample ();
        example.createCriteria ().andMyorderIdEqualTo (orderId);
        List<Flow> flowList = flowMapper.selectByExample (example);
        if (flowList != null&& flowList.size ()>0)
            return flowList.get (0);
        return null;
    }


}
